<?php
/**
 * 用户管理
 * User: ws
 * Date: 2018/7/26
 * Time: 15:19
 */

namespace app\admin\controller;
use think\Db;
use app\admin\model\User as UsModeL;
class User extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';

            $where['name|phone|unionid|openid'] = array('like','%'.trim($key).'%');

            $list = UsModeL::where($where)->order('user_id desc')->field('user_id,name,img,phone,time,leader,distribut_money,leader,unsettled_money,validity_time,whether_member,upper_level_id')->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$item){

                $log = db('user_card_log')->where('user_id',$item['user_id'])->where('validity_time','> time',time())->field('money,card_id')->order('card_log_id desc')->find();

                $list['data'][$key]['card_type'] = '无';
                $list['data'][$key]['money'] = '无';

                    if(!empty($log)){
                         $type = db('membership_card')->where('card_id',$log['card_id'])->value('type');
                        if($type==0){
                            $type = ' 年卡';
                        }elseif($type==1){
                            $type = '半年卡';
                        }elseif($type==2){
                            $type = '季卡';
                        }elseif($type==3){
                            $type =  '月卡';
                        }

                        $list['data'][$key]['card_type'] = $type;
                        $list['data'][$key]['money'] = $log['money'];
                    }


                    $list['data'][$key]['lower'] = UsModeL::where('user_id',$item['leader'])->value('name');
                    $list['data'][$key]['one'] = UsModeL::where('leader',$item['user_id'])->count();
                    $list['data'][$key]['two'] = UsModeL::where('upper_level_id',$item['user_id'])->count();
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function edit(){
        $list = UsModeL::where('user_id',input('id'))->find()->toArray();
        $materiel = self::choice($list['scope']);
        return view('edit',['list'=>$list,'materiel'=>$materiel]);
    }


    function lower(){
        if ($this->request->isAjax()) {
            $list = UsModeL::where('user_id', input('id'))->field('user_id,name,leader')->find()->toArray();
            $list['parentId'] = $list['leader'];
            unset($list['leader']);
            $list['children'] = $this->CycleData($list['user_id']);
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list]);die;
        }
        $id = $_GET['id'];
        return view('details',['id'=>$id]);
    }

    function CycleData($leader=0){
        $where['leader'] = $leader;
        $res = db('user')->where($where)->field('user_id,name,leader')->select();

        if(empty($res)) return false;

        foreach($res as $k=>$v){
            $result[$k]['user_id'] = $v['user_id'];
            $result[$k]['name'] = $v['name'];
            $result[$k]['parentId'] = $v['leader'];
            $re = $this->CycleData($v['user_id']);
            if($re!==false){
                $result[$k]['children'] = $re;
            }
        }
        return $result;
    }

    function choice(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name|phone'] = array('like','%'.trim($key).'%');
            $sj = input('sj')?input('sj'):'';
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $list = UsModeL::where($where)->order('user_id desc')->field('name,user_id,time,phone,img')->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('choice');
    }

    function choice_array(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name|phone'] = array('like','%'.trim($key).'%');
            $sj = input('sj')?input('sj'):'';
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $list = UsModeL::where($where)->order('user_id desc')->field('name,user_id,time,phone,img')->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('choice2');
    }



}